<?php


namespace common\helpers;


class RedisCache
{

    /**
     * @return \yii\redis\Connection
     */
    public static function redisClient()
    {
        return \Yii::$app->redis;
    }

    /**
     * 获取缓存
     * @param $key
     * @param bool $isDecode
     * @return mixed
     */
    public static function getCacheByKey($key, $isDecode = true)
    {
        $data = self::redisClient()->get(trim($key));
        if ($isDecode && !empty($data)) {
            return json_decode($data, true);
        }
        return $data;
    }

    /**
     * 设置缓存
     * @param $key
     * @param $data
     * @param $expire
     * @return mixed
     */
    public static function setCacheByKey($key, $data, $expire)
    {
        if (is_array($data) || is_object($data)) {
            $data = json_encode($data);
        }
        return self::redisClient()->setex($key, $expire, $data);
    }

    /**
     * 删除缓存
     * @param $key
     * @return mixed
     */
    public static function delCacheByKey($key)
    {
        return self::redisClient()->del($key);
    }


}